package com.oneday.nuo.base.config;

import com.oneday.nuo.base.exception.NuoException;
import com.oneday.nuo.base.response.NuoErrorStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
 * 统一异常处理
 * @Author: liushuai
 * @Date: 2021/3/15 20:39
 * @param : * @param null
 * @return :
 **/
@ControllerAdvice
@Slf4j
public class NuoExceptionHandler {

    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public String handleException(Exception ex) {
        if (ex instanceof NuoException) {
            log.warn(ex.getMessage(), ex);
            NuoException nuoException = (NuoException) ex;
            return nuoException.toString();
        } else {
            log.error(ex.getMessage(), ex);
            return  NuoErrorStatus.custom(ex.getMessage()).toString();
        }
    }

}